<?php
    cria_variaveis($_POST, $_GET, $_FILES);
?>
    <p class="titulo" align="center"><?php echo $msg->get("SISTEMAPF_MSG_02") ?></p>
    <p class="subtitulo" align="center"><?php echo $msg->get("SISTEMAPF_MSG_03") ?></p>
    <p>
    <div class="tabbar">
        <ul>
        <li><a href="index.php?inc=sistemaPF.php&modo=material"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material") echo " class=\"tabact\""; ?>><?php echo $msg->get("Material_Teorico") ?></a></li>
        <li><a href="index.php?inc=sistemaPF.php&modo=exemplo"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo") echo " class=\"tabact\""; ?>><?php echo $msg->get("Exemplos_Prontos") ?></a></li>
        <li><a href="index.php?inc=sistemaPF.php&modo=teclado"<?php if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado") echo " class=\"tabact\""; ?>><?php echo $msg->get("Entrada_Teclado") ?></a></li>
        </ul>
    </div>
    <div id="cas">
<?php
    if (!isset($Calcular))
    {
        /*****************************************
        * Caso nao haja nenhum modo escolhido,
        * e exibido o modo de material teorico
        ******************************************/
        if ($_SESSION['ANAWEB']->RetornaModoExecucao() == "material")
        {
            require_once("./classes/upload.php");
            $uploads = new Upload();
            $uploads->Lista("ptoflut");
        }
        /***********************************************
        * Para o modo de exemplos prontos, e incluido o
        * arquivo 'exibe_prob_ptoflut_sistema_xml.php'
        ************************************************/
        else if (($_SESSION['ANAWEB']->RetornaModoExecucao() == "exemplo"))
        {
            //leitura de dados via banco de dados - modulo de exemplos e exercicios
            include "exibe_prob_ptoflut_sistema_xml.php";
        }
        /***********************************************
        * Entrada de dados via teclado
        ************************************************/
        else if (($_SESSION['ANAWEB']->RetornaModoExecucao() == "teclado"))
        {
            if ((strlen($base) == 0)or($base < 2)or($base > 10)or(strlen($casas) == 0)or($casas < 1)or(strlen($expmin) == 0)or(strlen($expmax) == 0)or($expmin >= $expmax)or($casas <= 0)or($base <= 0))
            {
                unset($_SESSION['ANAWEB_PROBLEMA']);
?>
                <?php echo $msg->get("SISTEMAPF_MSG_04") ?>
                <?php echo $msg->get("SISTEMAPF_MSG_05") ?><br><br>
                <form method="post" enctype="multipart/form-data" action="index.php?inc=sistemaPF.php&modo=teclado">
                b = <input type="text" size="4" maxlenght="6" name="base">&nbsp;&nbsp;
                m = <input type="text" size="4" maxlenght="6" name="casas">&nbsp;&nbsp;
                e<sub>1</sub> = <input type="text" size="4" maxlenght="6" name="expmin">&nbsp;&nbsp;
                e<sub>2</sub> = <input type="text" size="4" maxlenght="6" name="expmax"><br><br>
<?php
                echo "<p>";
                 
                echo "<input class=button type=submit name=Calcular value=".$msg->get("G_MSG_04").">";
                echo "<br>";
                echo "</form>";
                echo "<form method=post action=index.php?opcao=sistemaPF>";
                 
                echo "<input class=button type=submit name=Submit value=".$msg->get("G_MSG_03").">";
                echo "</form>";
                echo "</body>";
            } //end if
        } //end else
    } // if (!isset($Calcular))
    /********************************************************************
    * Aqui acabam as partes dos menus e entrada dos dados, e inicia-se a
    * execucao dos calculos do sistema de ponto flutuante
    *********************************************************************/
    else
    {
        if (!checaNumerico($base) || !checaNumerico($casas) || !checaNumerico($expmin) || !checaNumerico($expmax))
        {
?>
        <p><font color="red"><?php echo $msg->get("Apenas_Dados_Numericos") ?></font></p>
        <p><input type="button" value="Voltar" onclick="history.back()" class="button"></p>
<?php
        }
        /*elseif (isset($base) && (($base < 2) or ($base > 7) ) )
        {
?>
        <p><font color="red"><?php echo SISTEMAPF_MSG_06 ?></font></p>
        <p><input type="button" value="Voltar" onclick="history.back()" class="button"></p>
<?php
        }
        elseif (isset($casas) && ($casas < 1 || $casas > 6))
        {
?>
        <p><font color="red"><?php echo SISTEMAPF_MSG_07 ?></font></p>
        <p><input type="button" value="Voltar" onclick="history.back()" class="button"></p>
<?php
        }
        elseif (isset($expmin) && isset($expmax) && ($expmax - $expmin) > 5 )
        {
?>
        <p><font color="red"><?php echo SISTEMAPF_MSG_08 ?></font></p>
        <p><input type="button" value="Voltar" onclick="history.back()" class="button"></p>
<?php
        }*/
        if ((2 * potencia($base, $casas-1) * ($base-1) * ($expmax-$expmin+1)+1) > 400)
        {
?>
        <p><font color="red"><?php echo $msg->get("Numeros_Demais"); ?></font></p>
        <p><font color="red"><?php echo $msg->get("Diminuir_Dados_PF") ?></font></p>
        <p><input type="button" value="Voltar" onclick="history.back()" class="button"></p>
<?php
        }
        else
        {
        $k = potencia($base, $casas-1);
        $j = 0;
        for ($i = 1; $i <= potencia($base, $casas); $i++)
        {
            $string[$i] = "";
        }
        //gera a combinacao dos numeros na base escolhida. exemplo, para base 2 e 2 casas decimais e criado 00, 01, 10,11
        while ($k >= 1 )
        {
            $j = 0;
            $variou = "nao";
            for ($i = 1; $i <= potencia($base, $casas); $i++)
            {
                if (($i-1)%$k == 0)
                    $variou = "nao";
                if (($i > $k) and ($variou == "nao") )
                {
                    $j++;
                    $variou = "sim";
                } //end if
                if ($j >= $base)
                {
                    $j = 0;
                    if (@$aumentou >= potencia($base, $casas)/$k)
                        $variou = "nao";
                } //end if
                $string[$i] .= "$j";
            } //end for i
            $k = $k/$base;
        }
        // end while
        $k = potencia($base, $casas-1); //casas-1 pois descarta as combinacoes que comecam com zero
        $tamanho = potencia($base, $casas)-$k; //tamanho do vetor com as combinacoes validas e igual ao numero de possibilidades menos o numero de combinacoes que comecam com zero
        for ($i = 1; $i <= $tamanho; $i++)
        {
            $stringf[$i] = '0.'.$string[$i+$k]; //cria o vetor das combinacoes de numeros possiveis
        } //end for
        $numaux2 = $expmin;
        $cont = 1;
        for ($i = 1; $i <= $tamanho; $i++)
        {
            //imprime na tela os calculos parciais
            $numaux = 1;
            $result[$i] = 0;
            for ($j = 2; $j < strlen($stringf[$i])-1; $j++)
            {
                // percorre cada algarismo da mantissa
                $result[$i] = $result[$i] + $stringf[$i][$j] / potencia($base, $numaux);
                $numaux++;
            } //end for
            $result[$i] = $result[$i] + $stringf[$i][$j] / potencia($base, $numaux);
        } //end for
        $passo = 0;

        if ($modo != "teclado")
            verificaBib($xml_file);
        echo "<br><b><p align=center>".$msg->get("SISTEMAPF_MSG_19")."</p></b>";
        echo "<br>";
        //imprime na tela os calculos parciais
?>
<table><tr>
<?php
        while ($expmax-$expmin >= $passo)
        {
            echo "<td><table bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"3\" border=\"0\">
                    <thead>
                        <tr>
                            <th colspan=\"2\" bgcolor=\"#ffffff\">".$msg->get("Expoente").": ".($expmin + $passo)."</td>
                        </tr>
                        <tr>
                            <th colspan=\"2\" bgcolor=\"#ffffff\">".$msg->get("SISTEMAPF_MSG_17")."</td>
                        </tr>
                    </thead>
                <tbody>";
            for ($i = 1; $i <= $tamanho; $i++)
            {
                //imprime na tela os calculos parciais
                $num[$cont] = $result[$i] * potencia($base, $numaux2);
                echo "<tr><td bgcolor=\"#ffffff\">$stringf[$i]*$base<sup>".($expmin+$passo)."</sup></td><td bgcolor=\"#ffffff\">".$num[$cont]."</td></tr>\n";
                $cont++;
            } //end for
            echo "\n
                <tr><td colspan=\"2\" bgcolor=\"#ffffff\">".$msg->get("SISTEMAPF_MSG_18").": ".($num[$cont-1] - $num[$cont-2])."</td></tr>
                </tbody></table></td>\n";
            $numaux2++;
            $passo++;
        }
?>
</table></tr>
<?php
        if ($expmax-$expmin < $passo)
        {
            echo "<br>";
            echo "<p align=center><b>".$msg->get("SISTEMAPF_MSG_09")."</b></p>";
            echo "-n = ".$msg->get("SISTEMAPF_MSG_10")." <br>";
            echo "- ".$msg->get("SISTEMAPF_MSG_11").": <br> #F = 2 * b<sup>n-1</sup> * (b - 1) * (e<sub>2</sub> - e<sub>1</sub> + 1) +1 = 2 *
                ".$base."<sup>".($casas-1)."</sup> * (".($base-1).") * (".($expmax-$expmin+1).") + 1 = <font color=blue>".(2 * potencia($base, $casas-1) * ($base-1) * ($expmax-$expmin+1)+1)."</font><br>";
            echo "- ".$msg->get("SISTEMAPF_MSG_12")." =  <font color=blue>".$num[1]."</font><br>";
            echo "- ".$msg->get("SISTEMAPF_MSG_13")." =  <font color=blue>".$num[$cont-1]."</font><br>";
            echo "- ".$msg->get("SISTEMAPF_MSG_14")." = <font color=blue>( ".-$num[1]." , ".$num[1]." )</font><br>";
            echo "- ".$msg->get("SISTEMAPF_MSG_15")." = <font color=blue>( -&infin; , ".-$num[$cont-1]." ) U ( ".$num[$cont-1]." , &infin; )</font><br><br>";
            echo "<p align=center><b>".$msg->get("SISTEMAPF_MSG_16")."</b></p>";
            echo "<p align=center>";
            echo "<table class=\"especial\" cellpadding=\"4\">";
            echo "<tr><td></td>";
            echo "<td colspan=\"".($expmax - $expmin +1)."\"><strong>".$msg->get("Expoente")."</strong></td>";
            echo "</tr>";
            echo "<tr>";
            echo "<td><strong>".$msg->get("Mantissa")."</strong></td>";
            for ($j = $expmin; $j <= $expmax; $j++)
                echo "<td>$j</td>";
            echo "</tr>";
            for ($i = 1; $i <= $tamanho; $i++)
            {
                echo "<tr>";
                echo "<td>$stringf[$i]</td>";
                for ($j = $expmin; $j <= $expmax; $j++)
                    echo "<td>".$num[ abs($expmin - $j) * $tamanho + $i ]."</td>";
                echo "</tr>";
            }
            echo "<tr>";
            echo "<td><strong>".$msg->get("SISTEMAPF_MSG_18")."</strong></td>";
            for ($j = $expmin; $j <= $expmax; $j++)
                echo "<td>".($num[ abs($expmin - $j) * $tamanho +2] - $num[ abs($expmin - $j) * $tamanho +1])."</td>";
            echo "</tr>";
            echo "</table>";
            $tam = ($expmax - $expmin)*$tamanho;
            $dados_grafico = "$tam|";
            $cont = 1;
            for ($j = $expmin; $j <= $expmax; $j++)
                for ($i = 1; $i <= $tamanho; $i++)
                {
                    $dados_grafico .= "$num[$cont];0|";
                    $cont++;
                }

            $arquivo = gera_nome_arquivo();
            $conn = &ADONewConnection($A_DB_TYPE);
            $conn->PConnect($A_DB_HOST, $A_DB_USER, $A_DB_PASS, $A_DB_DB);
            // $conn->debug=1;
            $sql = "INSERT INTO grafico(id,dados) values ('".$arquivo."','".$dados_grafico."');";
            $rs = $conn->Execute($sql);

            if ($rs === false) die(A_LANG_LOGIN_MSG5);
                $rs->Close();

            echo "</p><br><br>";
            echo "<table>";
            echo "<tr><td>".$msg->get("Distribuicao_Numeros")."</td></tr>";
            echo "<tr>";
            echo "<td><img src=sistemaPF_grafico.php?arquivo=".$arquivo." border=0 align=center width=1000 height=200></td>";
            echo "</table>";
            echo "<form method=post action=index.php?inc=sistemaPF.php>";
            echo "<input type=hidden name=arvore value=".@$arvore.">";
            echo "<br>";
            echo "<input class=button type=submit name=Submit value='".$msg->get("Voltar")."'>";
            echo "</form>";

        }
        if (isset($_POST['navegacao']) && $_POST['navegacao'] == 'sim')
        {
            $output = "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
            $output .= "<script language=\"javascript\" type=\"text/javascript\">\n";
            $output .= "function criarLinks(value) {\n";
            $output .= "document.exemplos.indice.value = value;\n";
            $output .= "document.exemplos.action = 'index.php?inc=sistemaPF.php&modo=exemplo'\n";
            $output .= "document.exemplos.submit();\n";
            $output .= "}\n";
            $output .= "</script>";
            $output .= "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
            $output .= "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
            $output .= "<input type=\"hidden\" name=\"indice\">";
            $output .= "<br><br>".$msg->get("Outros_Exemplos")."<br>";
            $objeto = DOMDocument::load($xml_file);
            $filho = $objeto->getelementsbytagname("floatingpoint");
            if ($filho->length)
            {
                $temp = $filho->item(0)->getelementsbytagname("floatp");
                for($i = 0; $i < $temp->length; $i++)
                    $output .= (($i!=0)?' | ':'').(($i==$indice)?'<b>':'')."<a href=\"javascript:criarLinks($i)\"><u>".($i+1)."</u></a>".(($i==$indice)?'</b>':'');
                echo $output;
            }
        }
        }
    } // end else Submit
?>

    </div>